package site.assad.basicQuartzTest;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;

import java.util.*;
import java.util.Calendar;

import static org.quartz.DateBuilder.dateOf;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2018/1/19 18:27
 * Description: 演示使用 Calendar 排除特殊日期
 */
public class CalendarExample {
    public static void main(String[] args) throws SchedulerException {

        //以下要执行一个任务，每1小时执行一次，将国庆节10.1-10.7排除在外

        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();

        JobDetail jobDetail = newJob(HelloJob.class)
                .withIdentity("job1","group1")
                .build();

        //设定 Calendar 集合
        AnnualCalendar holidays = new AnnualCalendar();
        ArrayList<Calendar> nationDayList = new ArrayList<>();
        for(int i=1;i<=7;i++){
            Calendar nationDay = new GregorianCalendar();
            nationDay.add(Calendar.MONTH,10);
            nationDay.add(Calendar.DATE,i++);
        }
        holidays.setDaysExcluded(nationDayList);  //设置每年执行集合的排除时间规则

        SimpleTrigger simpleTrigger = (SimpleTrigger) newTrigger()
                .withIdentity("job3","group1")
                .startAt(dateOf(19,5,0,19,1,2017))  //在 2017-1-19 19：05：00 开始执行
                .withSchedule(simpleSchedule()
                        .withIntervalInHours(1)
                        .repeatForever()
                )
                .build();

        scheduler.scheduleJob(jobDetail,simpleTrigger);
        scheduler.start();


    }
}
